<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form id="roleForm" class="definewidth m20" >
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td><input id="roleId" name="roleId" type="hidden"></td>
        <td width="10%" class="tableleft">角色名称</td>
        <td><input id="name" type="text" name="roleName" value="管理员" /></td>
    </tr>
    <tr>
        <td class="tableleft">状态</td>
        <td>
            <input type="radio" name="roleState" value="0" checked /> 启用
           <input type="radio" name="roleState" value="1"  /> 禁用
        </td>
    </tr>
    <tr>
        <td class="tableleft">权限</td>
        <td>-</td>
        <td id="resources">
		        <ul><label class='checkbox inline'><input type='checkbox' checked name='group[]' value='' />挂号信息管理</label></ul>
                <ul><label class='checkbox inline'><input type='checkbox'  checked name='group[]' value='' />门诊医生管理</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' checked  name='group[]' value='' />药品管理</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' checked  name='group[]' value='' />住院办理</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' checked  name='group[]' value='' />收费项目登记</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' checked  name='group[]' value='' />在院发药</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' checked  name='group[]' value='' />住院结算</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' checked  name='group[]' value='' />月营业额统计</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' name='group[]' value='' />年营业额统计</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' name='group[]' value='' />用户管理</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' name='group[]' value='' />角色管理</label></ul> 
                <ul><label class='checkbox inline'><input type='checkbox' name='group[]' value='' />资源管理</label></ul> 
		</td>
    </tr>
    <tr>
        <td class="tableleft"></td>
        <td>
            <button type="button" class="btn btn-primary" id="updateRole">更新</button> &nbsp;&nbsp;
            <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
        </td>
    </tr>
</table>
</form>
</body>
</html>
<script>
    $(function () {
        let url = document.location.toString();
        let index = url.GetValue("index");
        let id = url.GetValue("id");
        loadData();
        $(':checkbox[name="group[]"]').click(function () {
            $(':checkbox', $(this).closest('li')).prop('checked', this.checked);
        });

		$('#backid').click(function(){
				window.location.href="index.html?index="+index;
		 });

		$("#updateRole").click(function () {
            updateRole();
        })

        function updateRole(){
		    $.ajax({
                type:"POST",
                url:"/role",
                data: $("#roleForm").serialize()+"&_method=PUT",
                dataType:"json",
                success: function (vo) {
                        alert(vo.msg);
                        window.location.href="index.html?index="+index;
                }
            });
        }

		function loadData() {
            $.ajax({
                type:"GET",
                url:"/role/"+id,
                data:"",
                dataType:"json",
                success: function(vo){
                    if (vo.code==200){
                        let obj = vo.obj;
                        $("#roleId").val(obj.roleId);
                        $("#name").val(obj.roleName);
                        $($(".state").eq(obj.roleState)).prop('checked');
                        $.ajax({
                            type:"GET",
                            url:"/resource/parent",
                            data:"",
                            dataType:"json",
                            success: function (rvo) {
                                var list = rvo.list;
                                var selectedList = obj.list;
                                var str="";
                                $("#resources").html(str);
                                if (rvo.code=200){
                                    for (var i=0;i<list.length;i++){
                                        let re = list[i];
                                        let flag = true;
                                        for (let j=0;j<selectedList.length;j++){
                                            if (list[i].resourceId==selectedList[j].resourceId){
                                                str +=" <ul><label class='checkbox inline'><input type='checkbox' checked name='list1' value='"+ re.resourceId+"' />"+re.resourceName+"</label></ul> ";
                                                flag = false;
                                                break;
                                            }
                                        }
                                        if (flag){
                                            str +=" <ul><label class='checkbox inline'><input type='checkbox' name='list1' value='"+ re.resourceId+"' />"+re.resourceName+"</label></ul> ";
                                        }
                                    }
                                    $("#resources").html(str);
                                }else {
                                    alert("数据加载失败！");
                                }
                            }
                        });

                    }else {
                        alert(vo.msg);
                    }
                }
            });
        }
    });
</script>
